package com.meego.common.log;

import java.util.Enumeration;
import org.apache.log4j.Level;
import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;
import org.apache.log4j.MDC;
import org.apache.log4j.PropertyConfigurator;

public class GA {

    public static Logger app;
    public static Logger agent;
    public static Logger service;
    public static Logger jdbc;
    public static Logger audit;
    public static Logger pool;
    public static Logger config;
    public static Logger monitor;
    public static Logger monitor_stats;
    public static Logger sysmessage;
    public static Logger timer;
    public static Logger misc;
    public static Logger gm;
    public static Logger crypto;
    public static Logger billing;
    public static Logger plugin;
    public static Logger licence;
    public static Logger resource;
    public static Logger snmp;
    public static Logger cache;
    public static Logger mail;
    public static Logger urlrewrite;

    /**
     * Setting transid to MDC, and append to log with the pattern: time transid
     * [workername] log-level logger message
     *
     * @param trans
     */
    public static void setTrans(int trans) {
        MDC.put("trans", Integer.valueOf(trans));
    }

    /**
     * Setting transid to MDC, and append to log with the pattern: time transid
     * [workername] log-level logger message
     *
     * @param trans
     */
    public static void setTrans(long trans) {
        MDC.put("trans", Long.valueOf(trans));
    }

    /**
     * Clearing transid from MDC
     */
    public static void clearTrans() {
        MDC.remove("trans");
    }

    /**
     * Setting type to MDC
     *
     * @param type
     */
    public static void setType(String type) {
        if (type == null) {
            MDC.remove("type");
        } else {
            MDC.put("type", type);
        }
    }

    /**
     * Clearing type from MDC
     */
    public static void clearType() {
        MDC.remove("type");
    }

    @SuppressWarnings("unchecked")
    public static int count(Enumeration e) {
        int count = 0;
        while (e.hasMoreElements()) {
            ++count;
            e.nextElement();
        }
        return count;
    }

    public static synchronized void configure(String logPath) {
        try {
//			System.out.println("logPath: " + logPath);
            PropertyConfigurator.configure(logPath);
        } catch (Exception e) {
//			System.out.println("error: " + e.toString());
            e.printStackTrace();
            System.exit(1);
        }
//	}
//
//	static {
//		if (count(LogManager.getCurrentLoggers()) == 0) {
//			System.out.println("2: " + GA.logPath);
//			configure(logPath);
//			System.out.println("configure");
//		}

        app = LogManager.getLogger("ga.app");

        agent = LogManager.getLogger("ga.agent");

        service = LogManager.getLogger("ga.service");

        jdbc = LogManager.getLogger("ga.jdbc");

        audit = LogManager.getLogger("ga.audit");

        pool = LogManager.getLogger("ga.pool");

        config = LogManager.getLogger("ga.config");

        monitor = LogManager.getLogger("ga.monitor");

        monitor_stats = LogManager.getLogger("ga.monitor.stats");

        sysmessage = LogManager.getLogger("ga.sysmessage");

        timer = LogManager.getLogger("ga.timer");

        misc = LogManager.getLogger("ga.misc");

        gm = LogManager.getLogger("ga.gm");

        crypto = LogManager.getLogger("ga.crypto");

        billing = LogManager.getLogger("ga.billing");

        plugin = LogManager.getLogger("ga.plugin");

        licence = LogManager.getLogger("ga.licence");

        resource = LogManager.getLogger("ga.resource");

        snmp = LogManager.getLogger("ga.snmp");

        cache = LogManager.getLogger("ga.cache");
        
        mail = LogManager.getLogger("ga.mail");

        urlrewrite = LogManager.getLogger("ga.urlrewrite");
        
        audit.setAdditivity(false);
        monitor_stats.setAdditivity(false);
        monitor_stats.setLevel(Level.DEBUG);
    }
}
